package com.wechat.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;

/**
 *
 * @author cgc
 * @date 2018/4/17
 */
@Service
public class EncryptService {
    private static final Logger logger = LoggerFactory.getLogger(EncryptService.class);
    private String encoding = "UTF-8";
    private String algorithm = "AES";
    private SecretKeySpec skeySpec;
    private static final int KEY_LENGTH = 16;

    public EncryptService() {
    }

    public String encrypt(String value) {
        try {
            Cipher e = Cipher.getInstance(this.algorithm);
            e.init(1, this.skeySpec);
            return Hex.encodeHexString(e.doFinal(value.getBytes(this.encoding)));
        } catch (Exception var3) {
            logger.error("加密失败", var3);
            return null;
        }
    }

    public String decrypt(String value) {
        try {
            Cipher e = Cipher.getInstance(this.algorithm);
            e.init(2, this.skeySpec);
            return new String(e.doFinal(Hex.decodeHex(value.toCharArray())), this.encoding);
        } catch (Exception var3) {
            logger.warn("解密失败.", var3);
            return null;
        }
    }

    public String sha256(String password) {
        return this.hash(password, "SHA-256");
    }

    public String md5(String password) {
        return this.hash(password, "MD5");
    }

    public String sha1(String password) {
        return this.hash(password, "SHA-1");
    }

    public String sha512(String password) {
        return this.hash(password, "SHA-512");
    }

    public String hash(String password, String algorithm) {
        try {
            MessageDigest e = MessageDigest.getInstance(algorithm);
            byte[] message = password.getBytes(this.encoding);
            return Hex.encodeHexString(e.digest(message));
        } catch (Exception var5) {
            logger.warn("加密失败.", var5);
            return null;
        }
    }

    @Value("${security.key:QUM8GBGwrN9P5WJtnE08jQ==}")
    public void setEncryptionKey(String encryptionKey) throws Exception {
        byte[] bytes = Base64.decodeBase64(encryptionKey);
        byte[] key;
        if(bytes.length > KEY_LENGTH) {
            key = new byte[16];
            System.arraycopy(bytes, 0, key, 0, 16);
        } else {
            if(bytes.length != KEY_LENGTH) {
                throw new Exception("密钥长度有问题,请重新设置");
            }

            key = bytes;
        }

        this.skeySpec = new SecretKeySpec(key, this.algorithm);
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }

    public void initialize() throws Exception {
    }
}
